package cn.project.engineering.http

import com.google.gson.Gson
import com.google.gson.JsonSyntaxException
import okhttp3.ResponseBody
import retrofit2.Converter
import java.lang.reflect.Type

/**
 *
 * @author  lifuhai
 * @date 2021/2/18 15:06
 */
internal class CustomGsonResponseBodyConverter<T>(private val gson: Gson, private val type: Type) : Converter<ResponseBody, T> {
    override fun convert(value: ResponseBody): T? {
        val response = value.string()
        //数据解析，根据具体项目做处理
        return try {
            gson.fromJson(response, type)
        } catch (e: JsonSyntaxException) {
            e.printStackTrace()
            response as T
        }
    }
}